package com.whfc.ms.api.base.service.impl;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.whfc.base.dto.WeatherDTO;
import com.whfc.base.service.WeatherService;
import com.whfc.emp.service.AppEmpWarnService;
import com.whfc.entity.dto.warn.WarnCountDTO;
import com.whfc.env.service.EnvWarnDataService;
import com.whfc.fim.service.FimResultService;
import com.whfc.fuum.dto.AppProjectDTO;
import com.whfc.fuum.service.AppProjectService;
import com.whfc.mach.service.AppMachWarnService;
import com.whfc.ms.api.base.service.ProjectService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Service;
import java.util.Arrays;
import java.util.Date;


@Service
public class ProjectServiceImpl implements ProjectService {

    @DubboReference(interfaceClass = AppEmpWarnService.class, version = "1.0.0")
    private AppEmpWarnService appEmpWarnService;

    @DubboReference(interfaceClass = EnvWarnDataService.class, version = "1.0.0")
    private EnvWarnDataService envWarnDataService;

    @DubboReference(interfaceClass = FimResultService.class, version = "1.0.0")
    private FimResultService fimResultService;

    @DubboReference(interfaceClass = AppMachWarnService.class, version = "1.0.0")
    private AppMachWarnService appMachWarnService;

    @DubboReference(interfaceClass = AppProjectService.class, version = "1.0.0")
    private AppProjectService appProjectService;

    @DubboReference(interfaceClass = WeatherService.class, version = "1.0.0")
    private WeatherService weatherService;

    @Override
    public WarnCountDTO warnList(Integer deptId) {
        DateTime now = DateUtil.date();
        Date dateTimeStart = com.whfc.common.util.DateUtil.getDateBegin(now);
        Date dateTimeEnd = com.whfc.common.util.DateUtil.getDateEnd(now);
        Integer warnNumEmp = appEmpWarnService.getWarnNum(Arrays.asList(deptId), dateTimeStart, dateTimeEnd);
        Integer warnNumEnv = envWarnDataService.getWarnNum(Arrays.asList(deptId), null, dateTimeStart, dateTimeEnd);
        Integer warnNumFim = fimResultService.warnNumstatic(deptId, null, dateTimeStart, dateTimeEnd).stream().mapToInt(a -> a.getCount()).sum();
        Integer warnNumMach = appMachWarnService.warnNumStatic(deptId, null, dateTimeStart, dateTimeEnd).getWarnNum();
        return WarnCountDTO.builder().warnNumEmp(warnNumEmp).warnNumEnv(warnNumEnv).warnNumFim(warnNumFim)
                .warnNumTotal(warnNumMach + warnNumFim + warnNumEnv + warnNumEmp)
                .warnNumMach(warnNumMach).build();
    }


    @Override
    public WeatherDTO realtimeWeather(Integer deptId) {
        AppProjectDTO projectDTO = appProjectService.getByDeptId(deptId);
        Double lng = projectDTO.getLng();
        Double lat = projectDTO.getLat();
        if (lng != null && lat != null) {
            //刷新预报天气
            return weatherService.realtimeWeather(lng, lat);
        }
        return null;
    }

}
